#! /usr/bin/env bash

#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

TSQA_TSXS=${TSQA_TSXS:-/opt/ats/bin/tsxs}
TSQA_TESTNAME=$(basename $0)
source $(dirname $0)/functions

COUNT=${COUNT:-100}
SERVER_PORT=${SERVER_PORT:-4000}
RELOAD_INTERVAL_SECS=${RELOAD_INTERVAL_SECS:-30}

bootstrap

# Force a logging subsystem reload.
reload() {
  while sleep $RELOAD_INTERVAL_SECS ; do
    msg reloading logging configuration
    tsexec traffic_line -s proxy.config.log.hostname -v "$(date)"
    tsexec traffic_line -x
  done
}

if [ ! -x $(bindir)/jtest ] ; then
  fatal "missing jtest program; rebuild with --enable-test-tools"
fi

cat >$TSQA_ROOT/$(sysconfdir)/remap.config <<REMAP
map http://jtest.trafficserver.apache.org:$SERVER_PORT http://127.0.0.1:$SERVER_PORT
REMAP

# Configure the tcpinfo plugin so that we have some API log objects.
cat >$TSQA_ROOT/$(sysconfdir)/plugin.config <<REMAP
tcpinfo.so --log-file=tcpinfo1 --hooks=ssn_start,txn_start,send_resp_hdr,ssn_close,txn_close --log-level=2
tcpinfo.so --log-file=tcpinfo2 --hooks=ssn_start,txn_start,send_resp_hdr,ssn_close,txn_close --log-level=2
tcpinfo.so --log-file=tcpinfo3 --hooks=ssn_start,txn_start,send_resp_hdr,ssn_close,txn_close --log-level=2
tcpinfo.so --log-file=tcpinfo4 --hooks=ssn_start,txn_start,send_resp_hdr,ssn_close,txn_close --log-level=2
REMAP

# If Traffic Server is not up, bring it up ...
alive cop || startup || fatal unable to start Traffic Server
trap shutdown 0 EXIT

# Wait for traffic_manager to start.
alive manager
alive server
msgwait 2

# Logging configuration ...
tsexec traffic_line -s proxy.config.log.max_space_mb_for_logs -v 10
tsexec traffic_line -s proxy.config.log.max_space_mb_for_orphan_logs -v 10
tsexec traffic_line -s proxy.config.log.squid_log_enabled -v 1
tsexec traffic_line -s proxy.config.log.squid_log_is_ascii -v 1
# Roll every megabyte ...
tsexec traffic_line -s proxy.config.log.rolling_interval_sec -v 60
tsexec traffic_line -s proxy.config.log.rolling_size_mb -v 1
# Don't declare log space exhausted until there is < 1MB free.
tsexec traffic_line -s proxy.config.log.max_space_mb_headroom -v 1
# Flush log buffers every second.
tsexec traffic_line -s proxy.config.log.max_secs_per_buffer -v 1

# Enable logging diagnostics for test debugging. If you enable this
# for the test itself, the diagnostics log will exhaust all the logging
# space and everything will go to hell.
#tsexec traffic_line -s proxy.config.diags.debug.tags -v log

# The sleep is needed to let Traffic Server schedule the config change.
msgwait 4 to restart with updated logging configuration
# XXX: this needs a full bounce
tsexec traffic_line -L

# Wait for traffic_manager to start.
alive manager
alive server
msgwait 10

msg starting config reload every $RELOAD_INTERVAL_SECS seconds
reload&

for i in $(seq $COUNT) ; do
  msg $(bindir)/jtest --proxy_port $PORT --proxy_host 127.0.0.1 \
    --server_port $SERVER_PORT --server_host jtest.trafficserver.apache.org \
    --clients 10 --test_time 3600
  $(bindir)/jtest --proxy_port $PORT --proxy_host 127.0.0.1 \
    --server_port $SERVER_PORT --server_host jtest.trafficserver.apache.org \
    --clients 10 --test_time 60

  if [ -r $(logdir)/squid.log ]; then
    fatal squid.log is missing
  fi

  # Check for a crash ...
  crash
done

exit $TSQA_FAIL

# vim: set sw=2 ts=2 et :
